package com.lytoyo.neihanduanzi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lytoyo.neihanduanzi.aspect.Login;
import com.lytoyo.neihanduanzi.model.Result;
import com.lytoyo.neihanduanzi.model.blog.BlogComment;
import com.lytoyo.neihanduanzi.model.blog.BlogInfo;
import com.lytoyo.neihanduanzi.model.dto.BlogCommentDto;
import com.lytoyo.neihanduanzi.model.dto.BlogInfoDto;
import com.lytoyo.neihanduanzi.service.BlogCollectService;
import com.lytoyo.neihanduanzi.service.BlogCommentService;
import com.lytoyo.neihanduanzi.service.BlogService;
import com.lytoyo.neihanduanzi.utils.AuthContextHolder;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.simpleframework.xml.Path;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Package:com.lytoyo.neihanduanzi.controller
 *
 * @ClassName:BlogController
 * @Create:2025/3/3 12:14
 **/
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Resource
    private BlogService blogService;

    @Resource
    private BlogCommentService blogCommentService;

    @Resource
    private BlogCollectService blogCollectService;

    /**
     * 获取十条视频博客
     *
     * @return
     */
    @GetMapping("/v/getBlogList")
    public Result getBlogList(@RequestParam("current") Integer current, @RequestParam("size") Integer size) {
        Page<BlogInfoDto> blogPage = blogService.getBlogList(current, size);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records",blogPage.getRecords());
        map.put("current",blogPage.getCurrent());
        map.put("hasNext",blogPage.hasNext());
        return Result.success(map);
    }

    /**
     * 获取十条图文博客
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/p/getBlogList")
    public Result getPictureBlogList(@RequestParam("current")Integer current,@RequestParam("size")Integer size){
        Page<BlogInfoDto> blogPage = blogService.getPictureBlogList(current,size);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records",blogPage.getRecords());
        map.put("current",blogPage.getCurrent());
        map.put("hasNext",blogPage.hasNext());
        return Result.success(map);
    }

    /**
     * 给博客点赞
     *
     * @param blogId 博客Id
     * @param userId 点赞用户ID
     * @return
     */
    @Login
    @PostMapping("/blogClickLiked/{blogId}/{userId}")
    public Result blogClickLiked(@PathVariable("blogId") Long blogId, @PathVariable("userId") Long userId) {
        return blogService.blogClickLiked(blogId, userId);

    }

    /**
     * 取消博客点赞
     *
     * @param blogId
     * @param userId
     * @return
     */
    @Login
    @PostMapping("/blogCacelLiked/{blogId}/{userId}")
    public Result blogCacelLiked(@PathVariable("blogId") Long blogId, @PathVariable("userId") Long userId) {
        return blogService.blogCacelLiked(blogId, userId);
    }

    /**
     * 分页查询博客评论
     *
     * @param blogId
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/blogGainComment/{blogId}")
    public Result blogGainComment(@PathVariable("blogId") Long blogId, @RequestParam("current") Integer current, @RequestParam("size") Integer size) {
        Page<BlogComment> page = new Page<>(current, size);
        Map map = blogCommentService.gainComment(page, blogId);
        return Result.success(map);
    }

    /**
     * 添加博客评论
     *
     * @param blogCommentDto
     * @return
     */
    @Login
    @PostMapping("/blogAddComment")
    public Result blogAddComment(@RequestBody BlogCommentDto blogCommentDto) {
        return blogCommentService.addComment(blogCommentDto);
    }

    /**
     * 添加收藏
     *
     * @param blogId
     * @param userId
     * @return
     */
    @Login
    @PostMapping("/addCollectBlog/{blogId}/{userId}")
    public Result addCollectBlog(@PathVariable("blogId") Long blogId, @PathVariable("userId") Long userId) {
        return this.blogCollectService.addCollectBlog(blogId, userId);
    }

    /**
     * 取消博客收藏
     *
     * @param blogId
     * @param userId
     * @return
     */
    @Login
    @PostMapping("/cacelCollectBlog/{blogId}/{userId}")
    public Result cacelCollectBlog(@PathVariable("blogId") Long blogId, @PathVariable("userId") Long userId) {
        return this.blogCollectService.cacelCollectBlog(blogId, userId);
    }

    /**
     * 获取用户对视频的点赞收藏和评论的点赞情况
     *
     * @return
     */
    @Login
    @GetMapping("/v/getUserForBlogStatus")
    public Result getUserForBlogStatus() {

        Map map = this.blogService.getUserForBlogStatus();
        return Result.success(map);
    }

    /**
     * 获取子评论
     *
     * @param rootCommentId
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/v/getChildComments/{rootCommentId}")
    public Result getChildComments(@PathVariable("rootCommentId") Long rootCommentId, @RequestParam("current") Integer current, @RequestParam("size") Integer size) {
        Page<BlogComment> page = new Page<>(current, size);
        Map map = blogCommentService.gainChildComment(page, rootCommentId);
        return Result.success(map);
    }

    /**
     * 评论
     *
     * @param blogCommentDto
     * @return
     */
    @Login
    @PostMapping("/v/comment")
    public Result comment(@RequestBody BlogCommentDto blogCommentDto) {
        return this.blogCommentService.comment(blogCommentDto);
    }

    /**
     * 获取用户自己的作品
     *
     * @return
     */
    @Login
    @GetMapping("/mine/production")
    public Result production() {
        Long userId = AuthContextHolder.getUserId();
        return this.blogService.getMineProduction(userId);
    }

    /**
     * 查询自己收藏
     *
     * @return
     */
    @Login
    @PostMapping("/mine/collect")
    public Result collect(@RequestBody List<Long> blogIdList) {
        Long userId = AuthContextHolder.getUserId();
        return this.blogService.getMineCollect(userId, blogIdList);
    }

    /**
     * 查询自己喜欢
     *
     * @return
     */
    @Login
    @PostMapping("/mine/liked")
    public Result liked(@RequestBody List<Long> blogIdList) {
        Long userId = AuthContextHolder.getUserId();
        return this.blogService.getMineLiked(userId, blogIdList);
    }

    /**
     * 保存博客
     * @param blogInfo
     * @return
     */
    @Login
    @PostMapping("/save")
    public Result saveBlog(@RequestBody BlogInfo blogInfo){
        Long userId = AuthContextHolder.getUserId();
        return this.blogService.saveBlog(userId,blogInfo);
    }

    /**
     * 获取单条博客
     * @param blogId
     * @return
     */
    @GetMapping("/get/single")
    public Result getSingle(@RequestParam("blogId") Long blogId){
        return this.blogService.getSingleBlog(blogId);
    }
}
